package com.jinglvhuanjing.app.widget;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.google.android.exoplayer2.util.Log;
import com.jinglvhuanjing.app.R;
import com.jinglvhuanjing.app.SettingParamActivity;
import com.jinglvhuanjing.app.bean.CityBean;
import com.jinglvhuanjing.app.utils.Constants;
import com.jinglvhuanjing.app.utils.DateUtil;
import com.jinglvhuanjing.app.utils.QRCodeUtil;
import com.jinglvhuanjing.app.utils.SpUtils;

import java.util.Date;

public class LeftDataView extends ConstraintLayout {

    public LeftDataView(@NonNull Context context) {
        super(context);
        this.init(context);
    }

    public LeftDataView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    public LeftDataView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context);
    }

    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.view_left_data_layout , this , true);
        initData();
        setTime();
    }

    private void initData(){
        TextView temperature = findViewById(R.id.temperature);
        TextView temperature_des1 = findViewById(R.id.temperature_des1);
        TextView air_quality = findViewById(R.id.air_quality);
        TextView wind_des = findViewById(R.id.wind_des);

        ImageView register_code = findViewById(R.id.register_code);

        // 版本号
        TextView version_code = findViewById(R.id.version_code);
        version_code.setText(SpUtils.getString(getContext(), Constants.VERSION, "V2.0")+"\n版本号\n");

        version_code.setOnClickListener( view -> {
                nineClick();
            });
    }

    public void setTime(){
        initData();
        TextView week = findViewById(R.id.week);
        TextView calendar = findViewById(R.id.calendar);
        // 日期
        Date date = new Date(System.currentTimeMillis());
        week.setText(DateUtil.DateToWeek(date));
        calendar.setText(DateUtil.DateToYMD_HM(date));
    }

    public void setTitle(String title){
        TextView device_title = findViewById(R.id.device_title);
        device_title.setText("" + title);
    }

    public void setTianQi(CityBean cityBean){
        Log.e("MainActivity", "setTianQi");
        AppCompatTextView temperature = findViewById(R.id.temperature);
        AppCompatTextView temperature_des1 = findViewById(R.id.temperature_des1);
        AppCompatTextView air_quality = findViewById(R.id.air_quality);
        AppCompatTextView wind_des = findViewById(R.id.wind_des);

        // 当前气温
        temperature.setText(""+cityBean.getTem()+"℃");
        // 当前天气 最低气温~最高气温
        temperature_des1.setText(cityBean.getWea()+" "+cityBean.getTem2()+"~"+cityBean.getTem1());
        air_quality.setText("空气质量"+cityBean.getAir_level());
        wind_des.setText(cityBean.getWin()+" "+cityBean.getWin_speed());
    }

    public void setQrcodeSecond(String Content){
        ImageView device_code = findViewById(R.id.device_code);
        //二维码
        int h = QRCodeUtil.dip2px(getContext(), 60)*2;
        int w = QRCodeUtil.dip2px(getContext(), 60)*2;
        device_code.setImageBitmap(QRCodeUtil.createQRCodeBitmap(Content, w, h, "UTF-8","H", "1", Color.BLACK, Color.WHITE));
    }

    //点击9次
    private final int CLICK_NUM = 9;
    //点击时间间隔5秒
    private final int CLICK_INTERVER_TIME = 5000;
    //上一次的点击时间
    private long lastClickTime = 0;
    //记录点击次数
    private int clickNum = 0;

    /**
     * 点击9次
     */
    protected void nineClick() {
        //点击的间隔时间不能超过5秒
        long currentClickTime = SystemClock.uptimeMillis();
        if (currentClickTime - lastClickTime <= CLICK_INTERVER_TIME || lastClickTime == 0) {
            lastClickTime = currentClickTime;
            clickNum = clickNum + 1;
        } else {
            //超过5秒的间隔
            //重新计数 从1开始
            clickNum = 1;
            lastClickTime = 0;
            return;
        }
        if (clickNum == CLICK_NUM) {
            //重新计数
            clickNum = 0;
            lastClickTime = 0;
            /*实现点击多次后的事件*/
            SettingParamActivity.startActivity(getContext());
        }
    }
}
